#! /bin/bash

# Step1 refresh rpm and install software package
sudo DEBIAN_FRONTEND=noninteractive apt update
sudo DEBIAN_FRONTEND=noninteractive apt upgrade -y
sudo DEBIAN_FRONTEND=noninteractive apt install -y whiptail
sudo DEBIAN_FRONTEND=noninteractive apt install -y nginx libnginx-mod-rtmp ffmpeg


# Step2 write file 
sudo cat >> /etc/nginx/nginx.conf <<EOF
rtmp {
    server {
        listen 1935;  # RTMP Port

        application live {
            live on;
            record off;  # turnoff record

            # 允许所有人推流（可加鉴权）allow public ip
            allow publish all;
            allow play all;

            # 推流到 YouTube（中转）
            push rtmp://a.rtmp.youtube.com/live2/v0ht-835z-hksb-uhr3-fk8f;
        }
    }
}
EOF

# Step3 create fast commad

cat >/usr/local/bin/cys<<'EOF'
push_url1="$(whiptail --title "提示" --inputbox "请输入你要推>流的地址" 10 60 3>&1 1>&2 2>&3)"

push_url2="push $push_url1;"
sudo sed -i "s|push rtmp://a.rtmp.youtube.com/live2/.*|$push_url2|g" /etc/nginx/nginx.conf
EOF

sudo chmod +x /usr/local/bin/cys


if ! systemctl restart nginx;then
	whiptail --title "提示" --msgbox "rtmp中转服务器部署失败,按OK继续" 10 60
else 
	whiptail --title "提示" --msgbox "rtmp中转服务器部署成功.按OK继续" 10 60
fi
